/*
 * 컴파일을 위해 올려 놓은 소스 입니다.
 * 서버에 반영하지 마시고 다른국가에 적용할 경우 서버에 있는 class 를 카피해서 사용하십시오.
 * 
 */
package com.pushmsg.otaconf;


public class OtaConf
{
	private final int MAXMSGSIZE = 140;
	private OtaConfMsgHdr msghdr = null;
	private OtaConfWspHdr wsphdr = null;
	private wbXML body =null;
	private int segno=0;
	private byte[][] segment;
	
	public OtaConf(String fn, String suffix, String mode)
	{
		body = new wbXML(fn, suffix);
		msghdr = new OtaConfMsgHdr();
		wsphdr = new OtaConfWspHdr(mode);		
	}
	
	public boolean process()
	{		
		if(body.process()==false)
			return false;
		//System.out.println("OtaConf 21 : " + body.getLength());
		computeSegNo();
		segment = new byte[6][];

		makeSegment();	
		return true;	
	}
	
	public int getSegNum()
	{
		return segno;
	}
	public int getNumSegment()
	{
		return segno;
	}	
	public int  getHeadLength( )
	{
			//return wdpHdr.getLength()+wspHdr.getLength();
		//System.out.println( " wdpHdr.getLength()="+wdpHdr.getLength() );
		 
			return msghdr.getLength() ;
	}	
	
	private void computeSegNo()
	{
		System.out.println("*************");
		int bodylen = body.getLength();
		segno++;
		System.out.println("BODY LEN=" + bodylen);
		bodylen = bodylen - (140 - msghdr.getLength()-wsphdr.getLength());
		System.out.println("BODY LEN=" + bodylen);
		if(bodylen<0)
			segno--;	
		while(bodylen>(140-msghdr.getLength()))
		{
			System.out.println("BODY LEN=" + bodylen);
			segno++;			
			bodylen = bodylen - (140-msghdr.getLength());
		}
		segno++;
		System.out.println("Segment number=" + segno);	
		System.out.println("*************");
	}
	
	private void makeSegment()
	{
		int segndx=0, ndx=0, maxmsgsize=MAXMSGSIZE;
		int bodystart=0, bodylen = body.getLength();
		byte[] bodydata = body.getBody();
		byte[] buf = new byte[MAXMSGSIZE];
		msghdr.setTotalno(segno);
					
		while(true)
		{
			msghdr.setCurno(segndx+1);	
			for(int i=0; i<msghdr.getLength(); i++)
			{
				buf[ndx++]=msghdr.getData()[i];	
				maxmsgsize--;			
			}
			if(segndx==0)
			{
				for(int i=0; i<wsphdr.getLength(); i++)
				{
					buf[ndx++]=wsphdr.getData()[i];
					maxmsgsize--;
				}
			}
			for(int i=0; i<maxmsgsize&&i<bodylen; i++)
			{
				buf[ndx++]=bodydata[bodystart+i];
			}
			System.out.println("-----");
			System.out.println(segndx+" "+ndx);
			System.out.println("-----");
			segment[segndx]=new byte[ndx];
			for(int i=0; i<ndx; i++)
				segment[segndx][i]=buf[i];
			bodylen -= maxmsgsize;
			bodystart += maxmsgsize;
			ndx=0;
			if(bodylen<=0)
				break;
			maxmsgsize=MAXMSGSIZE;
			segndx++;				
		}
	}
	
	public byte[] getSegment(int ndx)
	{
		if(ndx+1>segno)
			return null;
		else
			return segment[ndx];
	}	
	
	public wbXML getBody() {
		if ( body == null) return null;
		else return body;
	}
	
	public byte[] getMsghdrBody(int total, int cur) {
		msghdr.setTotalno(total);
		msghdr.setCurno(cur);
		return msghdr.getData();
	}
	/**
	 * @return Returns the msghdr.
	 */
	public OtaConfMsgHdr getMsghdr() {
		return msghdr;
	}
	/**
	 * @param msghdr The msghdr to set.
	 */
	public void setMsghdr(OtaConfMsgHdr msghdr) {
		this.msghdr = msghdr;
	}
	/**
	 * @return Returns the wsphdr.
	 */
	public OtaConfWspHdr getWsphdr() {
		return wsphdr;
	}
	public byte[] getsetMsghdrBody(int total, int cur) {
		msghdr.setTotalno(total);
		msghdr.setCurno(cur);
		return msghdr.getData();
	}
	
	public byte[] getsetMsghdrBody() {
		return msghdr.getData();
	}	
}	
